"""
Case Type   : 资源池化集群系统故障模式库
Case Name   : 索引文件丢失
Create At   : 2025/04/20
Owner       : @kyleze
Description :
    1.创建数据库
    2.在数据库下创建表，并写入数据
    3.创建索引
    4.查看索引文件目录
    5.删除索引文件
    6.连接数据库，查询表
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.失败
    5.成功
    6.查询失败
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageFaultsMode(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")
        self.primary_root = Node("PrimaryRoot")
        self.db_name = "db_faults_test0122"
        self.table_name = "t_faults_test0122"
        self.index_name = "i_faults_test0122"

    def test_faults_mode(self):
        step = "--step1:创建数据库 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name};"
        db_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, db_res,
                      "执行失败:" + step)

        step = "--step2:在数据库下创建表，并写入数据 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"drop table if exists {self.table_name};" \
                  f"create table  {self.table_name}" \
                  f"(t_id integer, t_desc character(20));" \
                  f"insert into  {self.table_name} values " \
                  f"(generate_series(1,100000),'aaa');"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.logger.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      sql_res, "执行失败:" + step)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res,
                      "执行失败:" + step)

        step = "--step3:创建索引 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"create unique index {self.index_name} on " \
                  f"{self.table_name}(t_id);"
        self.logger.info(sql_cmd)
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.logger.info(sql_res)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, sql_res,
                      "执行失败:" + step)

        step = "--step4:查看索引文件目录 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"select pg_relation_filepath(" \
                  f"(select oid from pg_class where relname = " \
                  f"'{self.index_name}')::regclass);"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.logger.info(sql_res)
        self.assertIn("1 row", sql_res, "执行失败:" + step)
        path = sql_res.splitlines()[2].strip()
        self.logger.info(path)
        real_path = path.rsplit("/", 1)[0]
        self.logger.info(real_path)

        step = "--step5:删除索引文件 expect:成功--"
        self.logger.info(step)
        cmd = f"source {macro.DB_ENV_PATH}; " \
              f"dsscmd rm -p {real_path}/1 -l 100;" \
              f"dsscmd rm -p {real_path}/2 -l 100;" \
              f"dsscmd rm -p {real_path}/3 -l 100"
        self.logger.info(cmd)
        result = self.primary_node.sh(cmd).result()
        self.logger.info(result)
        self.assertIn("Success", result, "执行失败:" + step)

        step = "--step6:连接数据库，查询表 expect:查询失败--"
        self.logger.info(step)
        sql_cmd = f"select * from {self.table_name};\d"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.logger.info(sql_res)
        self.assertIn("ERROR:", sql_res, "执行失败:" + step)

        step = "--step7:清理环境 expect:成功--"
        self.logger.info(step)
        sql_cmd = f"drop database if exists {self.db_name};"
        sql_res = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      "执行失败:" + step)

    def tearDown(self):
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")